package com.example.minzhengtong

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.FragmentTransaction
import com.example.minzhengtong.base.BaseA
import com.example.minzhengtong.ui.*
import com.example.minzhengtong.ui.add.LoginA
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.SpUtil
import com.gyf.immersionbar.ImmersionBar
import kotlinx.android.synthetic.main.activity_main.*
import java.net.URL

class MainActivity : BaseA() {

    companion object {
        fun enterThis(context: Context) {
            var intent = Intent(context, MainActivity().javaClass)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showFragment(FRAGMENT_HOME)
        setToolBarVisiabale(false)
        initListener()
        if (!SpUtil.getInstance().existKey(Const.TOKEN)){
            LoginA.enterThis(baseContext)
        }else{
            HttpRequest().detail(baseContext)
        }
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).navigationBarColor(R.color.white).init()
    }

    override fun requestSuccess(json: String) {
        super.requestSuccess(json)
        LogUtil.d("获取详情成功$json")
    }

    private fun initListener(){
        home.setOnClickListener(this)
        second.setOnClickListener(this)
        third.setOnClickListener(this)
        four.setOnClickListener(this)
        five.setOnClickListener(this)
    }

    private var mHomeFG: HomeFG? = null
    private var mSecondFG: SecondFG? = null
    private var mThirdFG: ThirdFGNew? = null
    private var mFourFG: FourFG? = null
    private var mMineFG: MineFG? = null

    private val FRAGMENT_HOME = 0x01
    private val FRAGMENT_SECOND = 0x02
    private val FRAGMENT_THIRD = 0x03
    private val FRAGMENT_MINE = 0x05
    private val FRAGMENT_FOUR = 0x04

    private var mIndex = FRAGMENT_HOME

    override fun recreate() {
        try {
            val fragmentTransaction = supportFragmentManager.beginTransaction()
            if (mHomeFG != null) {
                fragmentTransaction.remove(mHomeFG!!)
            }
            if (mSecondFG != null) {
                fragmentTransaction.remove(mSecondFG!!)
            }
            if (mThirdFG != null) {
                fragmentTransaction.remove(mThirdFG!!)
            }
            if (mFourFG != null) {
                fragmentTransaction.remove(mFourFG!!)
            }
            if (mMineFG != null) {
                fragmentTransaction.remove(mMineFG!!)
            }
            fragmentTransaction.commitAllowingStateLoss()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        super.recreate()
    }

    /**
     * 隐藏所有的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mHomeFG?.let { transaction.hide(it) }
        mSecondFG?.let { transaction.hide(it) }
        mThirdFG?.let { transaction.hide(it) }
        mFourFG?.let { transaction.hide(it) }
        mMineFG?.let { transaction.hide(it) }
    }

    /**
     * 展示Fragment
     * @param index
     */
    private fun showFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        mIndex = index
        when (index) {
            // 首页
            FRAGMENT_HOME -> {
                if (mHomeFG == null) {
                    mHomeFG = HomeFG.newInstance()
                    transaction.add(R.id.container, mHomeFG!!, "home")
                } else {
                    transaction.show(mHomeFG!!)
                }
            }
            //
            FRAGMENT_SECOND -> {
                if (mSecondFG == null) {
                    mSecondFG = SecondFG.newInstance()
                    transaction.add(R.id.container, mSecondFG!!, "second")
                } else {
                    transaction.show(mSecondFG!!)
                }
            }
            //
            FRAGMENT_THIRD -> {
                if (mThirdFG == null) {
                    mThirdFG = ThirdFGNew.newInstance()
                    transaction.add(R.id.container, mThirdFG!!, "third")
                } else {
                    transaction.show(mThirdFG!!)
                }
            }
            //
            FRAGMENT_FOUR -> {
                if (mFourFG == null) {
                    mFourFG = FourFG.newInstance()
                    transaction.add(R.id.container, mFourFG!!, "four")
                } else {
                    transaction.show(mFourFG!!)
                }
            }
            //
            FRAGMENT_MINE -> {
                if (mMineFG == null) {
                    mMineFG = MineFG.newInstance()
                    transaction.add(R.id.container, mMineFG!!, "mine")
                } else {
                    transaction.show(mMineFG!!)
                }
            }
        }
        transaction.commit()
    }

    /**
     * 点击两次退出键,退出程序
     */
    private var mExitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 2000) {
                finish()
            } else {
                mExitTime = System.currentTimeMillis()
                showToast("再按一次退出程序")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.home -> {
                showFragment(FRAGMENT_HOME)
            }
            R.id.second -> {
                second.isChecked = false
                SecondA.enterThis(baseContext)
//                showFragment(FRAGMENT_SECOND)
            }
            R.id.third -> {
                showFragment(FRAGMENT_THIRD)
            }
            R.id.four -> {
                showFragment(FRAGMENT_FOUR)
            }
            R.id.five -> {
                showFragment(FRAGMENT_MINE)
            }
            else -> {
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (RESULT_OK == resultCode && 100 == requestCode){
            LoginA.enterThis(baseContext)
            finish()
        }
    }

}